home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / comm1 / s2mtr376.lha / SANA2Meter / Source / support.c < prev    next >
C/C++ Source or Header  |  1996-04-25  |  8KB  |  325 lines

  1. /*
  2. ** $VER: support.c 1.0 (25 Apr 1996)
  3. **
  4. ** (C) Copyright 1996 Marius Gröger
  5. **     All Rights Reserved
  6. **
  7. ** $HISTORY:
  8. **
  9. ** 25 Apr 1996 : 001.000 :  created
  10. */
  11.  
  12. /*F*/ /* includes */
  13.  
  14. #include <clib/alib_protos.h>
  15. #include <clib/dos_protos.h>
  16. #include <pragmas/dos_pragmas.h>
  17. #include <clib/utility_protos.h>
  18. #include <pragmas/utility_pragmas.h>
  19. #include <clib/exec_protos.h>
  20. #include <pragmas/exec_sysbase_pragmas.h>
  21. #include <clib/intuition_protos.h>
  22. #include <pragmas/intuition_pragmas.h>
  23. #include <clib/gadtools_protos.h>
  24. #include <pragmas/gadtools_pragmas.h>
  25. #include <clib/icon_protos.h>
  26. #include <pragmas/icon_pragmas.h>
  27. #include <clib/locale_protos.h>
  28. #include <pragmas/locale_pragmas.h>
  29.  
  30. #include <gtlayout/gtlayout.h>
  31.  
  32. #include <exec/libraries.h>
  33. #include <exec/devices.h>
  34. #include <exec/memory.h>
  35. #include <exec/io.h>
  36.  
  37. #include <workbench/startup.h>
  38.  
  39. #include <utility/tagitem.h>
  40.  
  41. #include <netinclude:devices/sana2.h>
  42.  
  43. #define AsmPools
  44. #include <pools/pool_lib.h>
  45.  
  46. #if (USE_EXTREADARGS != 0)
  47. #include <extrdargs.h>
  48. #endif
  49.  
  50. #include "compiler.h"
  51. #include "debug.h"
  52. #define CATCOMP_NUMBERS
  53. #include "locale.h"
  54. #include "sana2meter.h"
  55. #include "constdata.h"
  56.  
  57. #include <strings.h>
  58. /*E*/
  59.  
  60. /*F*/ /* private symbols */
  61. PRIVATE int ToolMatch(char *s1, char *s2);
  62. /*E*/
  63. /*F*/ /* exported symbols */
  64. PUBLIC VOID *allocpvec(GD gd, ULONG len);
  65. PUBLIC VOID freepvec(GD gd, VOID *mem);
  66. PUBLIC VOID *allocppvec(GD gd, ULONG len);
  67. PUBLIC VOID freeppvec(GD gd, VOID *mem);
  68. PUBLIC VOID message(GD gd, LONG, LONG, LONG, ...);
  69. PUBLIC struct Library *openlib(GD gd, STRPTR name, LONG version);
  70. PUBLIC char *AddToolType(GD gd, struct DiskObject *dobj, char *tool);
  71. PUBLIC ULONG strntolong(GD gd, STRPTR s, LONG *number, LONG n);
  72. /*E*/
  73. /*F*/ /* imported symbols */
  74. IMPORT STDARGS STRPTR GetString(struct LocaleInfo *li, LONG stringNum);
  75. /*E*/
  76.  
  77. /*F*/ PUBLIC ULONG strntolong(GD gd, STRPTR s, LONG *number, LONG n)
  78. {
  79.  
  80. #define IsDigit(c) ((c) >= '0' && (c) <= '9'))
  81. #define IsXDigit(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'A' && (c) <= 'F'))
  82.  
  83.    BOOL hex;
  84.    UBYTE c;
  85.    UWORD countdown;
  86.    ULONG preconv, rc;
  87.  
  88.    if ((n > 2) && !Strnicmp(s, "0x", 2))
  89.    {
  90.       hex = TRUE;
  91.       s += 2;
  92.       n -= 2;
  93.       preconv = 2;
  94.    }
  95.    else if ((n > 1) && !Strnicmp(s, "$", 1))
  96.    {
  97.       hex = TRUE;
  98.       ++s;
  99.       --n;
  100.       preconv = 1;
  101.    }
  102.    else
  103.    {
  104.       hex = FALSE;
  105.       preconv = 0;
  106.    }
  107.  
  108.    if (countdown=n)
  109.    {
  110.       LONG num;
  111.  
  112.       for(num=0; countdown && (*s != '\0'); ++s, --countdown)
  113.       {
  114.          c = ToUpper((ULONG)*s);
  115.          if (hex)
  116.          {
  117.             if (c >= '0' && c <= '9') num = num * 16 + c - '0';
  118.             else if (c >= 'A' && c <= 'F') num = num * 16 + c - '0';
  119.             else break;
  120.          }
  121.          else
  122.          {
  123.             if (c >= '0' && c <= '9') num = num * 10 + c - '0';
  124.             else break;
  125.          }
  126.       }
  127.  
  128.       if (n - countdown)
  129.       {
  130.          *number = num;
  131.          rc = preconv + (n - countdown);
  132.       }
  133.       else rc = 0;
  134.    }
  135.    else rc = 0;
  136.  
  137.    return rc;
  138. }
  139. /*E*/
  140.  
  141. /*F*/ PUBLIC VOID message(GD gd, LONG title, LONG body, LONG button, ...)
  142. {
  143.    struct EasyStruct es;
  144.  
  145.    es.es_StructSize = sizeof(struct EasyStruct);
  146.    es.es_Flags = 0;
  147.    es.es_Title = GetString(&gd->gd_LocaleInfo, title);
  148.    es.es_TextFormat = GetString(&gd->gd_LocaleInfo, body);
  149.    es.es_GadgetFormat = GetString(&gd->gd_LocaleInfo, button);
  150.  
  151.    if (gd->gd_Handle)
  152.    {
  153.       LT_LockWindow(gd->gd_Handle->Window);
  154.       EasyRequestArgs(gd->gd_Handle->Window, (struct EasyStruct *)&es, NULL, (APTR)(&button + 1));
  155.       LT_UnlockWindow(gd->gd_Handle->Window);
  156.    }
  157.    else
  158.    {
  159.       if (gd->gd_CLI || !IntuitionBase)
  160.       {
  161.          if (gd->gd_DOSBase) VPrintf(es.es_TextFormat, (APTR)(&button + 1));
  162.          VPrintf("\n", NULL);
  163.       }
  164.       else
  165.          EasyRequestArgs(NULL, (struct EasyStruct *)&es, NULL, (APTR)(&button + 1));
  166.    }
  167. }
  168. /*E*/
  169.  
  170. /*F*/ PUBLIC struct Library *openlib(GD gd, STRPTR name, LONG version)
  171. {
  172.    struct Library *lib;
  173.  
  174.    if (!(lib = OpenLibrary(name, version)))
  175.       message(gd, REQ_ERROR_TITLE, REQ_ERROR_OPENLIB, REQ_ERROR_QUITBUTTON,
  176.                                     name, version);
  177.    return lib;
  178. }
  179. /*E*/
  180.  
  181. /*F*/ PUBLIC VOID *allocpvec(GD gd, ULONG len)
  182. {
  183.    ULONG *p;
  184.  
  185.    if (p = AsmAllocPooled(gd->gd_AnyPool, len+sizeof(ULONG), (struct ExecBase*)SysBase))
  186.       *p++ = (ULONG)(len+sizeof(ULONG));
  187.  
  188.    return (VOID*)p;
  189. }
  190. /*E*/
  191. /*F*/PUBLIC VOID freepvec(GD gd, VOID *mem)
  192. {
  193.    ULONG *p = (ULONG*)mem - 1;
  194.  
  195.    AsmFreePooled(gd->gd_AnyPool, p, *p, (struct ExecBase*)SysBase);
  196.  
  197.    return ;
  198. }
  199. /*E*/
  200. /*F*/ PUBLIC VOID *allocppvec(GD gd, ULONG len)
  201. {
  202.    ULONG *p;
  203.  
  204.    if (p = AsmAllocPooled(gd->gd_PubPool, len+sizeof(ULONG), (struct ExecBase*)SysBase))
  205.       *p++ = (ULONG)(len+sizeof(ULONG));
  206.  
  207.    return (VOID*)p;
  208. }
  209. /*E*/
  210. /*F*/PUBLIC VOID freeppvec(GD gd, VOID *mem)
  211. {
  212.    ULONG *p = (ULONG*)mem - 1;
  213.  
  214.    AsmFreePooled(gd->gd_PubPool, p, *p, (struct ExecBase*)SysBase);
  215.  
  216.    return ;
  217. }
  218. /*E*/
  219.  
  220. /* ------------------------------------------------------------------------- */
  221. /* This code has been grabbed by Jure Vrhovnik's supra.lib library.          */
  222. /* I could not use the library since it assumes a global SysBase symbol      */
  223. /* Except of the PUBLIC identifier the routines below are _exactly_  the    */
  224. /* same as in supra.lib distribution 1.1, though.                            */
  225. /* A word to the AddToolType() call: This routine uses some undocumented     */
  226. /* information about the memory management of disk objects and might crash   */
  227. /* under future releases of icon.library.                                    */
  228. /*F*/ PRIVATE int ToolMatch(char *s1, char *s2)
  229. {
  230. int cmp = 2;
  231. int offs = 0;
  232.  
  233.     if ((*s1 == '(' || *s2 == '(') && *s1 != *s2) offs = 3;
  234.     if (*s1 == '(') s1++;
  235.     if (*s2 == '(') s2++;
  236.     if (*s1 == ')') {
  237.             if (*(s1+1) == '\0') s1++;
  238.     }
  239.  
  240.     if (*s2 == ')') {
  241.         if (*(s2+1) == '\0') s2++;
  242.     }
  243.  
  244.  
  245.     while (*s1 && *s2) {
  246.         if (*s1 == '\0' && *s2 == '\0') return(0+offs);
  247.         if (*s1 != *s2) return(cmp+offs);
  248.         if (*s1 == '=') cmp=1;
  249.         s1++; s2++;
  250.         if (*s1 == ')') {
  251.             if (*(s1+1) == '\0') s1++;
  252.         }
  253.  
  254.         if (*s2 == ')') {
  255.             if (*(s2+1) == '\0') s2++;
  256.         }
  257.  
  258.     }
  259.  
  260.     if (*s1 == '\0' && *s2 == '\0') return(0+offs);
  261.     else return(cmp+offs);
  262. }
  263. /*E*/
  264. /*F*/ PUBLIC char *AddToolType(GD gd, struct DiskObject *dobj, char *tool)
  265. {
  266. struct FreeList *flist = (struct FreeList *)dobj + sizeof(struct DiskObject);
  267. char ***ttypes = &(dobj->do_ToolTypes);
  268. char *item = (*ttypes)[0];
  269. int change = -1;
  270. int i=0;
  271. char *newmem;
  272. int len;
  273.  
  274.     while (item != NULL) {
  275.       if (change == -1) { /* We haven't found any matching tooltype yet */
  276.         switch(ToolMatch(item, tool)) {
  277.             case 0: return(item);   /* This tooltype already defined */
  278.             case 1:
  279.             case 3:
  280.             case 4: change = i;     /* Change this tooltype */
  281.         }   /* 2,5 means different */
  282.       }
  283.       i++;
  284.       item = (*ttypes)[i]; /* Get the next tooltype to be examined */
  285.     }
  286.  
  287.     if (change != -1) {
  288.         len = strlen(tool)+1;
  289.         if ((newmem = AllocMem(len, 0L)) == NULL) return(NULL);
  290.         else if (AddFreeList(flist, newmem, len) == FALSE) {
  291.             FreeMem(newmem, len);
  292.             return(NULL);
  293.         }
  294.  
  295.         strcpy(newmem, tool);
  296.         (*ttypes)[change] = newmem;
  297.         return(newmem);
  298.     } else {
  299.         len = (i+2)*4;
  300.         if ((newmem = AllocMem(len, 0L)) == NULL) return(NULL);
  301.         else if (AddFreeList(flist, newmem, len) == FALSE) {
  302.             FreeMem(newmem, len);
  303.             return(NULL);
  304.         }
  305.  
  306.         CopyMem(*ttypes, newmem+4, len);
  307.         *ttypes = (char **)newmem;
  308.  
  309.         len = strlen(tool)+1;
  310.         if ((newmem = AllocMem(len, 0L)) == NULL) return(NULL);
  311.         else if (AddFreeList(flist, newmem, len) == FALSE) {
  312.             FreeMem(newmem, len);
  313.             return(NULL);
  314.         }
  315.  
  316.         strcpy(newmem, tool);
  317.         (*ttypes)[0] = newmem;
  318.         (*ttypes)[i+1] = NULL;
  319.         return(newmem);
  320.     }
  321. }
  322. /*E*/
  323. /* ------------------------------------------------------------------------- */
  324.  
  325.